/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.topo.attachment;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.util.Date;

import twaver.AlarmSeverity;
import twaver.Link;
import twaver.Node;
import twaver.TDataBox;
import twaver.TUIManager;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.TaskAdapter;
import twaver.TaskScheduler;
import twaver.network.PaintAttachmentFilter;
import twaver.network.TNetwork;
import twaver.network.background.ImageBackground;
import twaver.network.ui.Attachment;
import demo.DemoPane;

public class AttachmentDemo extends DemoPane {
	
	static{
		TUIManager.registerAttachment("bar", BarAttachment.class);
		TUIManager.registerAttachment("pie", PieAttachment.class);
		TUIManager.registerAttachment("alarm", AlarmAttachment.class);
		TUIManager.registerAttachment("time", TimeAttachment.class);		

		TUIManager.registerAttachment("POSITION_LEFT", LeftAttachment.class);	
		TUIManager.registerAttachment("POSITION_RIGHT", RightAttachment.class);			
		TUIManager.registerAttachment("POSITION_CENTER", CenterAttachment.class);	
		
		TUIManager.registerAttachment("POSITION_TOP", LabelAttachment.class);	
		TUIManager.registerAttachment("POSITION_BOTTOM", LabelAttachment.class);	
		TUIManager.registerAttachment("POSITION_TOPLEFT", LabelAttachment.class);	
		TUIManager.registerAttachment("POSITION_TOPRIGHT", LabelAttachment.class);	
		TUIManager.registerAttachment("POSITION_BOTTOMLEFT", LabelAttachment.class);	
		TUIManager.registerAttachment("POSITION_BOTTOMRIGHT", LabelAttachment.class);	
		TUIManager.registerAttachment("POSITION_INNER_BOTTOM", LabelAttachment.class);	
		TUIManager.registerAttachment("POSITION_INNER_TOP", LabelAttachment.class);	
		TUIManager.registerAttachment("POSITION_INNER_LEFT", LabelAttachment.class);	
		TUIManager.registerAttachment("POSITION_INNER_RIGHT", LabelAttachment.class);	
		TUIManager.registerAttachment("POSITION_INNER_TOPLEFT", LabelAttachment.class);	
		TUIManager.registerAttachment("POSITION_INNER_TOPRIGHT", LabelAttachment.class);	
		TUIManager.registerAttachment("POSITION_INNER_BOTTOMLEFT", LabelAttachment.class);	
		TUIManager.registerAttachment("POSITION_INNER_BOTTOMRIGHT", LabelAttachment.class);	
	}
	
    TDataBox box = new TDataBox();
    TNetwork network = new TNetwork(box);

    public AttachmentDemo() throws Exception {
        this.add(network, BorderLayout.CENTER);  
        this.network.setToolbarByName(TWaverConst.DEFAULT_TOOLBAR);
        this.network.getDefaultOverview().getOverviewPane().setPaintAttachmentFilter(new PaintAttachmentFilter(){
			public boolean isPaintable(Attachment attachment) {
				return false;
			}
        });
        
        ImageBackground background = new ImageBackground("/demo/resource/images/usa.gif");
        background.setColor(Color.black);
		background.setGradientColor(Color.cyan);
		background.setGradientFactory(TWaverConst.GRADIENT_EXTEND_S);
		background.setGradient(true);
		this.network.setBackground(background);		
		
        final Node node1 = new Node("A");
        node1.setLocation(260, 250);
        node1.setName("Start");
        node1.setImage("/demo/resource/images/world.png");
        node1.getAlarmState().setAcknowledgedAlarmCount(AlarmSeverity.WARNING, 5);
        node1.addAttachment("pie");
        node1.addAttachment("time");
        node1.putBorderColor(Color.WHITE);
        box.addElement(node1);
        
        Node node2 = new Node("B");
        node2.setLocation(550, 380);
        node2.setName("End");
        node2.setImage("/demo/resource/images/world.png");
        node2.getAlarmState().addNewAlarm(AlarmSeverity.MINOR);
        node2.getAlarmState().addNewAlarm(AlarmSeverity.MINOR);
        node2.getAlarmState().addNewAlarm(AlarmSeverity.MINOR);
        node2.getAlarmState().acknowledgeAlarm(AlarmSeverity.MINOR);
        node2.putAlarmBalloonPosition(TWaverConst.POSITION_CENTER);
        node2.putAlarmBalloonDirection(TWaverConst.ATTACHMENT_DIRECTION_TOP_LEFT);
        node2.putAlarmBalloonYoffset(-10);
        node2.addAttachment("bar");
        node2.putBorderColor(Color.WHITE);
        box.addElement(node2);

        Link link = new Link(node1, node2);
        link.setLinkType(TWaverConst.LINK_TYPE_FLEXIONAL);
        link.setName("<html>Hello World<br>I'm flowing!</html>");
        link.putLinkFlowing(true);
        link.putLinkFlowingColor(Color.black);
        link.putLinkColor(Color.white);
        link.putLinkFlowingWidth(5);
        link.putLinkWidth(5);
        link.putLabelYOffset(-10);
        link.putLinkLabelRotatable(true);
        link.putLabelPosition(TWaverConst.POSITION_TOP);
        link.putLabelColor(Color.WHITE);
        link.putLabelFont(new Font("Forte", Font.PLAIN, 18));
        link.addAttachment("alarm");
        link.putBorderColor(Color.GREEN);
        box.addElement(link);
        
        final Node node3 = new Node();
        node3.setImage("/demo/network/topo/attachment/world.png");
        node3.putBorderColor(Color.YELLOW);
        node3.setLocation(150, 350);
        box.addElement(node3);
        
        addAttachment(node3, "POSITION_CENTER");		
		addAttachment(node3, "POSITION_TOP");	
		addAttachment(node3, "POSITION_BOTTOM");	
		addAttachment(node3, "POSITION_LEFT");	
		addAttachment(node3, "POSITION_RIGHT");	
		addAttachment(node3, "POSITION_TOPLEFT");	
		addAttachment(node3, "POSITION_TOPRIGHT");	
		addAttachment(node3, "POSITION_BOTTOMLEFT");	
		addAttachment(node3, "POSITION_BOTTOMRIGHT");	
		addAttachment(node3, "POSITION_INNER_BOTTOM");	
		addAttachment(node3, "POSITION_INNER_TOP");	
		addAttachment(node3, "POSITION_INNER_LEFT");	
		addAttachment(node3, "POSITION_INNER_RIGHT");	
		addAttachment(node3, "POSITION_INNER_TOPLEFT");	
		addAttachment(node3, "POSITION_INNER_TOPRIGHT");	
		addAttachment(node3, "POSITION_INNER_BOTTOMLEFT");	
		addAttachment(node3, "POSITION_INNER_BOTTOMRIGHT");	        
		
        TaskScheduler.getInstance().register(new TaskAdapter(){
			public void run(long clock) {
				node1.putClientProperty("currentTime", new Date());
				
				node3.putClientProperty("content:POSITION_TOP", getContent());
				node3.putClientProperty("content:POSITION_BOTTOM", getContent());
				node3.putClientProperty("content:POSITION_TOPLEFT", getContent());
				node3.putClientProperty("content:POSITION_TOPRIGHT", getContent());
				node3.putClientProperty("content:POSITION_BOTTOMLEFT", getContent());
				node3.putClientProperty("content:POSITION_BOTTOMRIGHT", getContent());
				node3.putClientProperty("content:POSITION_INNER_BOTTOM", getContent());
				node3.putClientProperty("content:POSITION_INNER_TOP", getContent());
				node3.putClientProperty("content:POSITION_INNER_LEFT", getContent());
				node3.putClientProperty("content:POSITION_INNER_RIGHT", getContent());
				node3.putClientProperty("content:POSITION_INNER_TOPLEFT", getContent());
				node3.putClientProperty("content:POSITION_INNER_TOPRIGHT", getContent());
				node3.putClientProperty("content:POSITION_INNER_BOTTOMLEFT", getContent());
				node3.putClientProperty("content:POSITION_INNER_BOTTOMRIGHT", getContent());
				
				node3.putClientProperty("color:POSITION_TOP", TWaverUtil.getRandomColor());
				node3.putClientProperty("color:POSITION_BOTTOM", TWaverUtil.getRandomColor());
				node3.putClientProperty("color:POSITION_TOPLEFT", TWaverUtil.getRandomColor());
				node3.putClientProperty("color:POSITION_TOPRIGHT", TWaverUtil.getRandomColor());
				node3.putClientProperty("color:POSITION_BOTTOMLEFT", TWaverUtil.getRandomColor());
				node3.putClientProperty("color:POSITION_BOTTOMRIGHT", TWaverUtil.getRandomColor());
				node3.putClientProperty("color:POSITION_INNER_BOTTOM", TWaverUtil.getRandomColor());
				node3.putClientProperty("color:POSITION_INNER_TOP", TWaverUtil.getRandomColor());
				node3.putClientProperty("color:POSITION_INNER_LEFT", TWaverUtil.getRandomColor());
				node3.putClientProperty("color:POSITION_INNER_RIGHT", TWaverUtil.getRandomColor());
				node3.putClientProperty("color:POSITION_INNER_TOPLEFT", TWaverUtil.getRandomColor());
				node3.putClientProperty("color:POSITION_INNER_TOPRIGHT", TWaverUtil.getRandomColor());
				node3.putClientProperty("color:POSITION_INNER_BOTTOMLEFT", TWaverUtil.getRandomColor());
				node3.putClientProperty("color:POSITION_INNER_BOTTOMRIGHT", TWaverUtil.getRandomColor());
			}
        });
    }

    private void addAttachment(Node node, String attachmentName){
    	node.addAttachment(attachmentName);	
    	Node n = new Node();
    	n.setName(attachmentName);
    	n.setSelected(true);
    }
    
    private Object getContent(){
    	return new Character((char) ('A' + TWaverUtil.getRandomInt(26))).toString()
    	+ new Character((char) ('A' + TWaverUtil.getRandomInt(26))).toString()
    	+ new Character((char) ('A' + TWaverUtil.getRandomInt(26))).toString();
    }
    
}